<?php

namespace  app\api\validate;

use app\admin\model\CourseEnroll;
use app\admin\model\CourseOrder;
use app\admin\model\ExperCourse;
use app\common\model\Bank as BankModel;
use app\common\model\CarersApply;
use app\common\model\Course;
use app\common\model\User;
use think\Validate;

class Carers extends Validate
{
    protected $rule = [
        'user_id' => 'checkUser',
        'realname|姓名' => 'require',
        'mobile|手机号' => 'require',
        'city_id|城市'    => 'require',
        'address|详细地址' => 'require',
        'idcard|身份证号码' => 'require|checkIdcard',
        'bank_id|银行卡ID' => 'require|checkBank',
        'money|提现金额' => 'require|checkMoney',
        'course_id|教学课程ID' => 'require|checkCourseID',
        'exper_course_id|体验课程ID' => 'require|checkExperCourseID',
    ];

    protected $scene = [
        'apply' => ['user_id','realname','mobile','city_id','address'], //申请成为陪诊师
        'auth'  => ['realname','idcard'],  //实名认证
        'withdrawals' => ['bank_id','money'],   //提现
        'buy_course' => ['course_id'],  //购买教学课程
        'enroll_course' => ['exper_course_id','realname','mobile']  //报名体验课程
    ];

    public function checkCourseID($value, $rule, $data)
    {
        if (!Course::get($value)) {
            return '教学课程不存在';
        }

        //查询是否已经付费该课程
        if (CourseOrder::where(['user_id' => $data['user_id'], 'course_id' => $value, 'pay_status' => 1])->find()) {
            return  '你已付费该课程，请勿重复付费';
        }

        return true;
    }

    public function checkExperCourseID($value, $rule, $data)
    {
        if (!ExperCourse::get($value)) {
            return '体验课程不存在';
        }

        //查询是否已经报名该课程
        if (CourseEnroll::where(['user_id' => $data['user_id'], 'course_id' => $value])->find()) {
            return  '你已报名该课程，请勿重复报名';
        }

        return true;
    }


    public function checkMoney($value, $rule, $data)
    {
        $user = User::get($data['user_id']);
        if ($user['money'] < $value) {
            return '余额不足';
        }

        return true;
    }

    public function checkBank($value, $rule, $data)
    {
        if (!BankModel::where('user_id', $data['user_id'])->find($value)) {
            return '银行卡不存在';
        }

        return true;
    }

    public function checkUser($value, $rule, $data)
    {
        $user = User::get($value);

        if ($user['level']) {
            return '您已经成为陪诊师,不能重新发起申请';
        }

        if (CarersApply::where(['user_id' => $value, 'status' => 0])->find()) {
            return '您有未处理的申请，请耐心等待';
        }

        return true;
    }

    public function checkIdcard($value, $rule, $data)
    {
        if (!$this->isCreditNo($value)) {
            return '身份证号码格式有误';
        }

        return true;
    }

    /**
     * 判断是否为合法的身份证号码
     * @param $mobile
     * @return int
     */
    public function isCreditNo($vStr){
        $vCity = array(
            '11','12','13','14','15','21','22',
            '23','31','32','33','34','35','36',
            '37','41','42','43','44','45','46',
            '50','51','52','53','54','61','62',
            '63','64','65','71','81','82','91'
        );
        if (!preg_match('/^([\d]{17}[xX\d]|[\d]{15})$/', $vStr)) return false;
        if (!in_array(substr($vStr, 0, 2), $vCity)) return false;
        $vStr = preg_replace('/[xX]$/i', 'a', $vStr);
        $vLength = strlen($vStr);
        if ($vLength == 18) {
            $vBirthday = substr($vStr, 6, 4) . '-' . substr($vStr, 10, 2) . '-' . substr($vStr, 12, 2);
        } else {
            $vBirthday = '19' . substr($vStr, 6, 2) . '-' . substr($vStr, 8, 2) . '-' . substr($vStr, 10, 2);
        }
        if (date('Y-m-d', strtotime($vBirthday)) != $vBirthday) return false;
        if ($vLength == 18) {
            $vSum = 0;
            for ($i = 17 ; $i >= 0 ; $i--) {
                $vSubStr = substr($vStr, 17 - $i, 1);
                $vSum += (pow(2, $i) % 11) * (($vSubStr == 'a') ? 10 : intval($vSubStr , 11));
            }
            if($vSum % 11 != 1) return false;
        }
        return true;
    }
}